import { _decorator, Button, Label, Sprite, SpriteAtlas, Toggle } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { Respones, RewardType } from '../../Config/GameCfg';
import { IAsTzEndGetRewardRes } from '../proto/gameTz';
const { ccclass, property } = _decorator;

/**
 * 每日挑战有奖励
 */
@ccclass('GameEndTzWin_Ctr')
export class GameEndTzWin_Ctr extends UIComponent {
    spIcon: Sprite;
    lab: Label;
    isClick: boolean = true;
    tog: Toggle;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.spIcon = this.ViewComponent('root/logo/icon',Sprite);
        this.lab = this.ViewComponent('root/logo/lab',Label);

        this.AddButtonListener('root/btn1',this,this.OnBtnGetClick);

        this.tog = this.ViewComponent('root/layout/Toggle',Toggle);

    }

    private InitData(){

    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgStartAnswerTzEndGetRewardRes,this.onStartAnswerTzEndGetRewardRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgStartAnswerTzEndGetRewardRes,this.onStartAnswerTzEndGetRewardRes,this);
    }

    private OnBtnGetClick(btn:Button){
        if(this.isClick === false){
            return;
        }
        this.isClick = false;
        let bei =  this.tog.isChecked == true ? 2 : 1;

        globalThis.gameMgr._GameTzMsg.StartAnswerTzEndGetRewardReq(bei);
    }

    //结算领取奖励返回
    private onStartAnswerTzEndGetRewardRes(data: IAsTzEndGetRewardRes){
        console.log('======onStartAnswerTzEndGetRewardRes=======');
        if(this.hasProInObj(data,'code')){
            if(data.code == Respones.OK){
                this.node.destroy();
            }
        }
    }

    public setDate(data:any){
        console.log('=======GameEndTzWin_Ctr=setDate======',data);
        let atals = ResManager.inst.getAsset(AbNames.GamePlay,'Atals/GamePlay',SpriteAtlas) as SpriteAtlas;

        let sf = null;
        if(data.rewardType == RewardType.Type_Quan){
            sf = atals.getSpriteFrame('GameEndTz/GameEndTz_icon');
        }
        if(sf){
            this.spIcon.spriteFrame = sf;
        }
        this.lab.string = `x${data.rewardNum}`;
    }
}


